/*----------------------------------------------------------------------------
  Owner(s): Shane Whitfield
$Copyright: $
       $Id: $

 Purpose  : 
----------------------------------------------------------------------------*/
#ifndef __SINGLETON_H__
#define __SINGLETON_H__

#include "core.h"

/*--------------------------------------------------------------------------*/
//
// This singleton code was taken from Scott Bilas' example at LCS.
//
template <typename T>
class TSingleton
{
protected:
	typedef T SingletonType;

	TSingleton()
	{
		ptrdiff_t offset = (ptrdiff_t)(T*)1 - (ptrdiff_t)(TSingleton <T> *)(T*)1;
		s_singleton = (T*)((ptrdiff_t)this + offset);
	}

	~TSingleton()
	{
		s_singleton = NULL;
	}

	// special case use only!!
	void clearSingleton()
	{
		s_singleton = NULL;
	}

public:
	static T*   GetSingletonPtr()	 { return s_singleton; }
	static T&   GetSingleton()		 { return *s_singleton; }
	static bool DoesSingletonExist() { return s_singleton != NULL; }

private:
	static T* s_singleton;
};

template <typename T> T* TSingleton <T>::s_singleton = NULL;

//EXAMPLE Exposure
//inline CCameraMgr& CCameraMgr_GetSingleton() { return CCameraMgr::GetSingleton(); }
//#define g_cameramgr (CCameraMgr_GetSingleton())

/*--------------------------------------------------------------------------*/

#endif // __SINGLETON__H__

/*-----------------------------------EOF!-----------------------------------*/ 
